<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet
    version='1.0'
    xmlns:xalan="http://xml.apache.org/xalan"
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

    <xsl:output method="html" indent="yes" xalan:indent-amount="4"/>

    <!-- ======================================================== -->
    <xsl:template match="/">
        <xsl:variable name="jobname" select="builds/job/title"/>
        <xsl:variable name="capjobname"
                      select="concat(
                                  translate( substring( $jobname, 1, 1 ),
                                             'abcdefghijklmnopqrstuvwxyz',
                                             'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ),
                                  substring( $jobname, 2 ) ) "/>
        <html>
        <head>
            <title> <xsl:value-of select="$capjobname"/> Build Status </title>
            <style type="text/css">
                @import url( /common.css );
                .passed { background-color: #00ff7f; }
                .failed { background-color: #ff7070; }
                .failedtest { background-color: #ffff00; }
                .running { background-color: #ccccff; }
                .idle { background-color: #ffff00; }
                .spacer { width: 3em; }
                .red { background-color: #ff7070; }
                .green { background-color: #00ff7f; }
                .blue { background-color: #ccccff; }
                .jobtitle { font-size: 200% }
                .jobinfo { font-weight: bold; }
                .promoted { font-weight: bold;
                            color: #FFF;
                            background-color: #00F; }
                .nowrap { white-space: nowrap; }

                td.time { text-align: center; }
                td.date { font-weight: bold; }
                td.logo { align: left; text-align: left; }
                td.rlogo { align: right; text-align: right; }
                th { font-weight: bold; text-align: center; }
            </style>
            <meta http-equiv="Refresh" content="60"/>
        </head>
        <body>
            <table width="100%">
            <tr>
                <td class="logo">
                    <img src="/icons/sct_logo.gif" alt="LOGO" align="middle"/>
                </td>
                <td class="rlogo">
                    <a href=""><img src="/icons/cabie.gif"
                                     width="150"
                                     height="90"
                                     alt="Continuous Automated Build and Integration Environment"/></a>
                </td>
            </tr>
            </table>

            <table class="jobinfo">
                <tr>
                    <td class="jobtitle" colspan="3">
                        <xsl:value-of select="$capjobname"/> Build Status
                    </td>
                </tr>
                <tr>
                    <xsl:choose>
                        <xsl:when test="/builds/job/jobstate = 'enabled'">
                            <td>&#160;&#160;&#160;&#160;</td>
                            <td class="green">&#160;&#160;&#160;</td>
                            <td>Build job is enabled</td>
                        </xsl:when>
                        <xsl:otherwise>
                            <td>&#160;&#160;&#160;&#160;</td>
                            <td class="red">&#160;&#160;&#160;</td>
                            <td>Build job is disabled</td>
                        </xsl:otherwise>
                    </xsl:choose>
                </tr>
                <tr>
                    <xsl:choose>
                        <xsl:when test="string-length( /builds/job/pending ) = 0">
                            <td>&#160;&#160;&#160;&#160;</td>
                            <td class="blue">&#160;&#160;&#160;</td>
                            <td>No changes are pending</td>
                        </xsl:when>
                        <xsl:otherwise>
                            <td>&#160;&#160;&#160;&#160;</td>
                            <td class="green">&#160;&#160;&#160;</td>
                            <td>Changes are pending</td>
                        </xsl:otherwise>
                    </xsl:choose>
                </tr>
                <xsl:if test="/builds/job/info[ status = 'building' ]">
                    <xsl:variable name="total-seconds">
                        <xsl:value-of select="sum( /builds/job/info[status != 'building' and status != 'failed' ]/duration )"/>
                    </xsl:variable>
                    <xsl:variable name="num-jobs">
                        <xsl:value-of select="count( /builds/job/info[ status != 'building' and status != 'failed' ]/duration )"/>
                    </xsl:variable>
                    <xsl:variable name="job-avg">
                        <xsl:choose>
                            <xsl:when test="$num-jobs = '0'"> 7800 </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="$total-seconds div $num-jobs"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:variable name="cur-duration">
                        <xsl:choose>
                            <xsl:when test="/builds/job/info[ status = 'building' ]/duration">
                                <xsl:value-of select="/builds/job/info[ status = 'building' ]/duration"/>
                            </xsl:when>
                            <xsl:otherwise> 0 </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:variable name="remaining">
                        <xsl:value-of select="$job-avg - $cur-duration"/>
                    </xsl:variable>

                    <tr>
                        <td/>
                        <td colspan="2">
                            <xsl:choose>
                                <xsl:when test="$remaining &gt;= 0">
                                    <xsl:variable name="num">
                                        <xsl:value-of select="ceiling( $remaining div 60 )"/>
                                    </xsl:variable>
                                    <xsl:value-of select="$num"/>
                                    <xsl:call-template name="minutes">
                                        <xsl:with-param name="num" select="$num"/>
                                    </xsl:call-template>
                                    until the job completes
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:variable name="num">
                                        <xsl:value-of select="ceiling( - $remaining div 60 )"/>
                                    </xsl:variable>
                                    the job is
                                    <xsl:value-of select="$num"/>
                                    <xsl:call-template name="minutes">
                                        <xsl:with-param name="num" select="$num"/>
                                    </xsl:call-template>
                                    overdue
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                    </tr>
                </xsl:if>
            </table>

            <p/>

            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th class="spacer"/>
                        <th>Build</th>
                        <th class="spacer"/>
                        <th/>
                        <th class="spacer"/>
                        <th colspan="3" align="center">Linux</th>
                        <th class="spacer"/>
                        <th colspan="3" align="center">Solaris</th>
                        <th class="spacer"/>
                        <th colspan="3" align="center">Windows</th>
                    </tr>
                    <tr>
                        <th>&amp;&#160;Time</th>
                        <th class="spacer"/>
                        <th>Number</th>
                        <th class="spacer"/>
                        <th>Build</th>
                        <th class="spacer"/>
                        <th>Install</th>
                        <th>Unit</th>
                        <th>Smoke</th>
                        <th class="spacer"/>
                        <th>Install</th>
                        <th>Unit</th>
                        <th>Smoke</th>
                        <th class="spacer"/>
                        <th>Install</th>
                        <th>Unit</th>
                        <th>Smoke</th>
                    </tr>
                </thead>

                <xsl:for-each select="/builds/job/info">

                    <!-- If this node's start day is not the same as the
                         preceding start day, add a blank row to separate
                         the days.  Note: This only works if the XML file
                         is sorted by build number since preceding-sibling
                         operates on the physical representation of the tree
                         rather that on the result of a sort operation in
                         the for-each statement.
                    -->

                    <xsl:if test="not( preceding-sibling::info[ 1 ] ) or preceding-sibling::info[ 1 ]/start/@dayofyear != start/@dayofyear">
                        <tr> <td> &#160; </td> </tr>
                        <tr>
                            <td class="date" colspan="3">
                            <xsl:variable name="dayname">
                                <xsl:call-template name="day-of-week">
                                    <xsl:with-param name="node" select="start"/>
                                </xsl:call-template>
                            </xsl:variable>

                            <xsl:value-of select="$dayname"/>&#160;<xsl:value-of select="start/@year"/>&#x2011;<xsl:value-of select="start/@month"/>&#x2011;<xsl:value-of select="start/@day"/>
                            </td>
                        </tr>
                    </xsl:if>

                    <tr>
                        <td class="time">
                            <xsl:value-of select="concat( start/@hour, ':', start/@minute )"/>
                        </td>
                        <td class="spacer"/>
                        <td>
                            <span class="nowrap">
                            <xsl:choose>
                                <xsl:when test="promotioninfo">
                                    <xsl:value-of select="num"/>
                                    &#160;
                                    <span class="promoted">&#160;P&#160;</span>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="num"/>&#160;&#160;
                                </xsl:otherwise>
                            </xsl:choose>
                            </span>
                        </td>
                        <td class="spacer"/>
                        <xsl:call-template name="build-status"/>
                        <td class="spacer"/>
                        <xsl:call-template name="platform-status">
                            <xsl:with-param name="platform" select="'Linux'"/>
                        </xsl:call-template>
                        <td class="spacer"/>
                        <xsl:call-template name="platform-status">
                            <xsl:with-param name="platform" select="'Solaris'"/>
                        </xsl:call-template>
                        <td class="spacer"/>
                        <xsl:call-template name="platform-status">
                            <xsl:with-param name="platform" select="'Windows'"/>
                        </xsl:call-template>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
        </html>
    </xsl:template>

    <!-- ======================================================== -->
    <!-- Render the build cell for one job.
     -->
    <xsl:template name="build-status">
        <xsl:choose>
            <xsl:when test='status = "building"'>
                <xsl:call-template name="cell">
                    <xsl:with-param name="text">building</xsl:with-param>
                    <xsl:with-param name="class">running</xsl:with-param>
                    <xsl:with-param name="duration" select="duration"/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="''"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test='status = "failed"'>
                <xsl:call-template name="cell">
                    <xsl:with-param name="text">failed</xsl:with-param>
                    <xsl:with-param name="class">failed</xsl:with-param>
                    <xsl:with-param name="duration" select="duration"/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="''"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test='status = "failedtest"'>
                <xsl:call-template name="cell">
                    <xsl:with-param name="text">failedtest</xsl:with-param>
                    <xsl:with-param name="class">failedtest</xsl:with-param>
                    <xsl:with-param name="duration" select="duration"/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="''"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="cell">
                    <xsl:with-param name="text">passed</xsl:with-param>
                    <xsl:with-param name="class">passed</xsl:with-param>
                    <xsl:with-param name="duration" select="duration"/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="''"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- ======================================================== -->
    <!-- Render the Install, Unit, and Smoke columns in the table.
         platform - the platform to render: Linux, Solaris, or Windows
      -->
    <xsl:template name="platform-status">
        <xsl:param name="platform"/>

        <xsl:variable name="lowplat"
                      select='translate( $platform,
                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                                         "abcdefghijklmnopqrstuvwxyz" )'/>
        <xsl:choose>
            <xsl:when test='status = "building"'>
                <td class="running"> &#160; </td>
                <td class="running"> &#160; </td>
                <td class="running"> &#160; </td>
            </xsl:when>

            <xsl:when test='status = "failed"'>
                <td class="failed" align="center"> not&#160;run </td>
                <td class="failed" align="center"> not&#160;run </td>
                <td class="failed" align="center"> not&#160;run </td>
            </xsl:when>

            <xsl:when test='teststatus/test[platform=$platform]/installing/resultcode = "failed"'>
                <xsl:call-template name="status-cell">
                    <xsl:with-param name="status"   select='teststatus/test[platform=$platform]/installing/resultcode'/>
                    <xsl:with-param name="duration" select='teststatus/test[platform=$platform]/installing/duration'/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="concat( 'testreport/', $lowplat, '/installing.log' )"/>
                    <xsl:with-param name="mime"     select="'text/plain'"/>
                </xsl:call-template>
                <td class="failed" align="center"> not&#160;run </td>
                <td class="failed" align="center"> not&#160;run </td>
            </xsl:when>

            <xsl:otherwise>

                <xsl:call-template name="status-cell">
                    <xsl:with-param name="status"   select='teststatus/test[platform=$platform]/installing/resultcode'/>
                    <xsl:with-param name="duration" select='teststatus/test[platform=$platform]/installing/duration'/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="concat( 'testreport/', $lowplat, '/installing.log' )"/>
                    <xsl:with-param name="mime"     select="'text/plain'"/>
                </xsl:call-template>

                <xsl:call-template name="status-cell">
                    <xsl:with-param name="status"   select='teststatus/test[platform=$platform]/unittest/resultcode'/>
                    <xsl:with-param name="duration" select='teststatus/test[platform=$platform]/unittest/duration'/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="concat( 'testreport/', $lowplat, '/unittest.log' )"/>
                    <xsl:with-param name="mime"     select="'text/plain'"/>
                </xsl:call-template>

                <xsl:call-template name="status-cell">
                    <xsl:with-param name="status"   select='teststatus/test[platform=$platform]/uitest/resultcode'/>
                    <xsl:with-param name="duration" select='teststatus/test[platform=$platform]/uitest/duration'/>
                    <xsl:with-param name="baseurl"  select="buildurl"/>
                    <xsl:with-param name="suburl"   select="concat( 'testreport/', $lowplat )"/>
                </xsl:call-template>

            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- ======================================================== -->
    <xsl:template name="status-cell">
        <xsl:param name="status"/>
        <xsl:param name="duration"/>
        <xsl:param name="baseurl"/>
        <xsl:param name="suburl"/>
        <xsl:param name="mime"/>
        <xsl:choose>
            <xsl:when test="$status != '' and $status != 'idle'">
                <xsl:call-template name="cell">
                    <xsl:with-param name="text"     select="$status"/>
                    <xsl:with-param name="class"    select="$status"/>
                    <xsl:with-param name="duration" select="$duration"/>
                    <xsl:with-param name="baseurl"  select="$baseurl"/>
                    <xsl:with-param name="suburl"   select="$suburl"/>
                    <xsl:with-param name="mime"     select="$mime"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="cell">
                    <xsl:with-param name="text"  select="'not&#160;tested'"/>
                    <xsl:with-param name="class" select="'idle'"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- ======================================================== -->
    <!-- Render one colored cell in the table.
         text     - text to display in cell
         class    - CSS style to apply to cell, usually "pass" or "fail"
         duration - how long the action took to run
         baseurl  - optional URL link to a build/install/test log
         suburl   - optional URL link to a build/install/test log
         mime     - the MIME type of the link target
     -->
    <xsl:template name="cell">
        <xsl:param name="text"/>
        <xsl:param name="class"/>
        <xsl:param name="duration"/>
        <xsl:param name="baseurl"/>
        <xsl:param name="suburl"/>
        <xsl:param name="mime"/>

        <xsl:variable name="cell-text">
            <xsl:choose>
                <xsl:when test="string-length( $duration ) = 0 or $duration = '0'"><xsl:value-of select="$text"/></xsl:when>
                <xsl:otherwise><xsl:value-of select="ceiling( number( $duration ) div 60 )"/>&#160;min</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="align">
            <xsl:choose>
                <xsl:when test="string-length( $duration ) = 0"><xsl:value-of select="'center'"/></xsl:when>
                <xsl:otherwise><xsl:value-of select="'right'"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <td>
            <xsl:attribute name="class">
                <xsl:value-of select="$class"/>
            </xsl:attribute>
            <xsl:attribute name="align">
                <xsl:value-of select="$align"/>
            </xsl:attribute>

            <xsl:choose>
                <xsl:when test='string-length( $baseurl ) = 0'>
                    &#160;<xsl:value-of select="normalize-space( $cell-text )"/>&#160;
                </xsl:when>
                <xsl:otherwise>
                    &#160;<a>
                        <xsl:if test="$mime">
                            <xsl:attribute name="type">
                                <xsl:value-of select="$mime"/>
                            </xsl:attribute>
                        </xsl:if>
                        <xsl:attribute name="href">
                            <xsl:value-of select="concat( $baseurl, '/', $suburl )"/>
                        </xsl:attribute><xsl:value-of select="$cell-text"/></a>&#160;
                </xsl:otherwise>
            </xsl:choose>
        </td>
    </xsl:template>

    <!-- ======================================================== -->
    <xsl:template name="day-of-week">
        <xsl:param name="node"/>
        <xsl:choose>
            <xsl:when test="$node/@dayofweek = '0'">Sun</xsl:when>
            <xsl:when test="$node/@dayofweek = '1'">Mon</xsl:when>
            <xsl:when test="$node/@dayofweek = '2'">Tue</xsl:when>
            <xsl:when test="$node/@dayofweek = '3'">Wed</xsl:when>
            <xsl:when test="$node/@dayofweek = '4'">Thu</xsl:when>
            <xsl:when test="$node/@dayofweek = '5'">Fri</xsl:when>
            <xsl:when test="$node/@dayofweek = '6'">Sat</xsl:when>
        </xsl:choose>
    </xsl:template>

    <!-- ======================================================== -->
    <!-- Output "minute" when num == 1 and "minutes" otherwise" -->
    <xsl:template name="minutes">
        <xsl:param name="num"/>
        <xsl:choose>
            <xsl:when test="$num = 1"> minute </xsl:when>
            <xsl:otherwise> minutes </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
